﻿using System;
using Xunit;
using System.Windows.Forms;
using ColorCode;

namespace AnglicanGeek.LiveWriter
{
    public class CodeColorizerPluginFacts
    {
        public class The_CreateContent_method
        {
            [Fact]
            public void Will_show_the_insert_source_code_form()
            {
                var stubDialogOwner = new StubDialogOwner();
                var stubFormFactory = StubFormFactory.Create();
                var plugin = new CodeColorizerPlugin(stubFormFactory);
                string content = null;
                
                plugin.CreateContent(stubDialogOwner, ref content);

                Assert.Same(stubDialogOwner, stubFormFactory.StubInsertSourceCodeDialog.Show__DialogOwnerArg);
            }

            [Fact]
            public void Will_center_the_insert_source_code_form()
            {
                var stubDialogOwner = new StubDialogOwner();
                var stubFormFactory = StubFormFactory.Create();
                var plugin = new CodeColorizerPlugin(stubFormFactory);
                string content = null;

                plugin.CreateContent(stubDialogOwner, ref content);

                Assert.Equal(FormStartPosition.CenterParent, stubFormFactory.StubInsertSourceCodeDialog.StartPosition__SetValue);
            }

            [Fact]
            public void Will_set_the_available_language_choices()
            {
                var stubDialogOwner = new StubDialogOwner();
                var stubFormFactory = StubFormFactory.Create();
                var plugin = new CodeColorizerPlugin(stubFormFactory);
                string content = null;

                plugin.CreateContent(stubDialogOwner, ref content);

                foreach (var language in Languages.All)
                    Assert.Contains(language, stubFormFactory.StubInsertSourceCodeDialog.SetLanguageChoices__LanguagesArg);
            }

            [Fact]
            public void Will_return_null_content_when_the_dialog_is_cancelled()
            {
                var stubDialogOwner = new StubDialogOwner();
                var stubFormFactory = StubFormFactory.Create();
                stubFormFactory.StubInsertSourceCodeDialog.Show__Return = DialogResult.Cancel;
                var plugin = new CodeColorizerPlugin(stubFormFactory);
                string content = null;

                plugin.CreateContent(stubDialogOwner, ref content);

                Assert.Null(content);
            }

            [Fact]
            public void Will_colorize_source_code_using_chosen_language_when_the_dialog_result_is_OK()
            {
                var stubDialogOwner = new StubDialogOwner();
                var stubFormFactory = StubFormFactory.Create();
                stubFormFactory.StubInsertSourceCodeDialog.Show__Return = DialogResult.OK;
                stubFormFactory.StubInsertSourceCodeDialog.Language__GetValue = Languages.JavaScript;
                stubFormFactory.StubInsertSourceCodeDialog.SourceCode__GetValue = "var fnord = \"This is not the source code you are looking for.\";";
                var plugin = new CodeColorizerPlugin(stubFormFactory);
                string content = null;

                plugin.CreateContent(stubDialogOwner, ref content);

                Assert.Equal(new CodeColorizer().Colorize("var fnord = \"This is not the source code you are looking for.\";", Languages.CSharp), content);
            }
        }
    }
}
